﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO.Ports;
using RS232;
using DatabaseHelper;

namespace UserControlLibrary
{
    public partial class RS232View : UserControl
    {
        public RS232Reader Reader { get; private set; }
        public PData DataNow { get; private set; }
            
        public RS232View()
        {
            InitializeComponent();
            DataNow = new PData();
            Reader = new RS232Reader();
            foreach (string str in SerialPort.GetPortNames())
            {
                cbbComPort.Items.Add(str);
            }
            string[] rates = new string[] { "75", "110", "300", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200"};
            foreach (string str in rates)
            {
                cbbBaudRate.Items.Add(str);
            }
            cbbComPort.Text = "COM2";
            cbbBaudRate.SelectedIndex = 6;
            txtStringName.Text = "Panther";
            txtStringFormat.Text = "StartString,KP,Easting,Northing,CP,";
        }

        private void Reader_RS232DataReceived(object sender, RS232DataReceivedEventArgs e)
        {
            DataNow = new PData();
            DataNow = e.Data;
            SetText(DataNow);
        }

        private void SetText(PData pdt)
        {
            txtKP.Text = pdt.KP;
            txtEasting.Text = pdt.Easting;
            txtNorthing.Text = pdt.Northing;
            txtCP.Text = pdt.CP;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            Open();
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            SetComPort();
        }

        private void RS232View_Load(object sender, EventArgs e)
        {
            SetComPort();
        }

        private void SetComPort()
        {
            if (!Reader.IsOpen)
            {
                try
                {
                    Reader.Port.PortName = cbbComPort.Text;
                    Reader.Port.BaudRate = int.Parse(cbbBaudRate.Text);
                    Reader.StringFormat = txtStringFormat.Text;
                    Reader.StringName = txtStringName.Text;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
        public void Open()
        {
            if (btnTest.Text == btnTest.Text1)
            {
                Reader.RS232DataReceived += new EventHandler<RS232DataReceivedEventArgs>(Reader_RS232DataReceived);
                Reader.Open();
                btnSet.Enabled = false;
            }
            else
            {
                Reader.Close();
                btnSet.Enabled = true;
            }
        }

        public void StartLogging(string recordID)
        {
            if (!Reader.IsOpen)
            {
                Open();
            }
            Reader.StartLogging(recordID);
            btnSet.Enabled = false;
            btnTest.Enabled = false;
        }

        public void StopLogging()
        {
            Reader.StopLogging();
            btnSet.Enabled = true;
            btnTest.Text = btnTest.Text2;
            btnTest.Enabled = true;
        }
    }
}
